package com.bawei.module_car_space.ui

import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.RequestEvent
import com.bawei.lib_common.base.state.Success
import com.bawei.module_car_space.model.EditModel
import com.bawei.module_car_space.model.SpaceModelItem
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject


@HiltViewModel
class SpaceViewModel @Inject  constructor(private val repository: SpaceRepository) : BaseViewModel() {

    var mSpaceListData = listOf<SpaceModelItem>()

    val mSpace_ = MutableStateFlow<RequestEvent>(Loading)
    val mSpaceState = mSpace_.asStateFlow()

    fun getSpaceData(spaceType: String) {
        execute(
            action = repository.getSpace(spaceType),
            onStart = {
                mSpace_.value = Loading
            },
            onError = {
                mSpace_.value = Failed(it)
            },
            onFinish = {
                mSpaceListData = it.data.reversed()
                mSpace_.value = Success
            }
        )
    }
    var mEditListData = listOf<EditModel>()
    val mEdit_ = MutableStateFlow<RequestEvent>(Loading)
    val mEditState = mEdit_.asStateFlow()

    fun addSpaceData(editModel: EditModel) {
        execute(
            action = repository.addSpace(editModel),
            onStart = {
                mEdit_.value = Loading
            },
            onError = {
                mEdit_.value = Failed(it)
            },
            onFinish = {
                getSpaceData(editModel.spaceType)
                mEdit_.value = Success
            }
        )
    }





}